function createTable(ID)
{
	var lastsel2=0;
	var lastsel=0;

	jQuery("#table"+ID).jqGrid({
	   	url:'/admin/show/'+ID,
	    editurl:'/admin/edittour/'+ID,
		datatype: "json",
	    mtype: 'POST',
	   	colNames:['ID',name,desc,'',''],
	   	colModel:[
	   		{name:'id',index:'id', width:55, align:"center",key:true,editable:false},
	   		{name:'name',index:'name', width:250,editable:true,editrules:{required:true}},
	   		{name:'desc',index:'desc', width:700,editable:true,edittype:'textarea',editrules:{required:true}},
	   		{name:'edit',index:'edit', width:95, align:"center",hidden:ID==4?true:false,editable:false},
	   		{name:'go',index:'go', width:45, align:"center",hidden:ID>3?true:false,editable:false}
	   	],
	   	rowNum:50,
	   	rowList:[10,20,30,40,50,100,300,500,1000000],
	   	pager: '#pager'+ID,
	   	sortname: 'id',
	    viewrecords: true,
	    sortorder: "asc",
	    caption:ID,
	    subGrid: false,
	    onSelectRow: function (id) {
	                if (id && id !== lastsel2) {
	                	//////console.log("lastsel "+lastsel);
	                   	jQuery('#table'+ID).jqGrid('restoreRow', lastsel2);
	                	jQuery('#table'+ID).jqGrid('editRow', id, true);
	                    lastsel2 = id;
	                }
	            },
	    width:"1200",
	    height:"400",
	    loadError:function(xhr,status,error)
	    {
	    	//alert("Произошла ошибка загрузки информации "+status+" - "+error);
	    	if($('#nameError'))
	    	{
		    	$('#nameError').empty().append(status);
		    	$('#errorMessage').empty().append(error);
		    	$('.error').toggle();
	    	}
	    	else
	    	{
	    		alert(error);
	    	}
	    },
	    gridComplete:function()
	    {
	    	$('.edit_row_tourlist').click(function(){
				var id=$(this).attr('id');
				var type=$(this).attr('mtype');
				showOptions(type,id);
			});
	    }});
	jQuery("#table"+ID).jqGrid('navGrid','#pager'+ID,{edit:true,add:true,del:true})
		.navButtonAdd(
			'#pager'+ID,
			{
           		caption:"Clone", 
           		buttonicon:"ui-icon-plusthick", 
           		onClickButton: function()
           			{
           				if($(this).getGridParam('selrow')==null)
           				{
           					$('#masterError')
           					.css('left',($(window).width()/2+$(window).scrollLeft()-78)+'px')
           					.css('top',($(window).height()/2+$(window).scrollTop()-43)+'px')
           					.find('#errorMessage')
           					.append("Выберите запись")
           					.end()
           					.find('#nameError')
           					.append("Ошибка")
           					.end()
           					.toggle();
           				}
           				else
           				{
           					var id=$(this).getGridParam('selrow');
           					
           					$('#clone')
           					.css('left',($(window).width()/2+$(window).scrollLeft()-78)+'px')
           					.css('top',($(window).height()/2+$(window).scrollTop()-43)+'px')
           					.find('#cloneId')
           					.val(id)
           					.end()
           					.find('#tableid')
           					.attr('tableid',ID)
           					.end()
           					.toggle();
           				}
           				
           			}, 
           		position:"last"
            });
	
}
function loadList(type,id,where,how)
{
	
	$(where).empty();
	$.ajax({
		url: '/admin/getlist/'+type+'/'+id,
		type:'post',
		success:function(data)
			{
				
				////////console.log(data);
				//data=eval('('+data+')');
				if(data.state==true)
					{
						//$('#mWFormId','.modalW2').append(data.context);
						////////console.log(data);
						switch(how)
						{
							case 'select':
								var select=$('<select id="option_t" name="option['+type+']" ></select>');
								for(row in data.data)
								{
									$(select).append('<option value='+row+' '+
												(data.data[row]==true?'selected':'')+
												'>'+row+'</option>');
									////////console.log(row);
									////////console.log(data.data[row]);
								}
								$(where).append(select);
							break;
							case 'checkbox':
								var list=$('<div id=option_t'+type+'></div>');
								for (row in data.data)
								{
									$(list).append('<input type=checkbox name=option['+type+']['+row+'] id=option_'+row+' value='+row+' '+
												(data.data[row]['checked']==true?'checked':'')+
												'> '+data.data[row]['name']+'<br/>');
								}
								$(where).append(list);
							break;
							case 'radio':
								var list=$('<div id=option_t'+type+'></div>');
								for (row in data.data)
								{
									$(list).append('<input type=radio id=option name=option['+type+'] value='+row+' '+
												(data.data[row]['checked']==true?'checked':'')+
												'> '+data.data[row]['name']+'<br/>');
								}
								$(where).append(list);
							break;
							case 'line':
								var line=$('<input type=text name=option['+type+'] />');
								$(line).val(data.data);
								$(where).append(line);
							break;
							case 'text':
								var line=$('<textarea rows=4 cols=55 name=option['+type+']>'+data.data+'</textarea>');
								//$(line).append(data.data);
								//////console.log(data.data);
								$(where).append(line);
							break;
							case 'picto':
								var div=$('<div id=option'+type+'></div>');
								$(div).append('<input type=hidden name=option['+type+'][0]>')
								$(where).append(div);
								for(row in data.data)
								{
									var t=data.data[row]['checked']==true?'checked="checked"':'';
									$(where).append('  <div class="picto '+data.data[row]['name']+' '+data.data[row]['checked'] +
													'" alt="'+data.data[row]['desc']+'">' +
													'<input ' +
													'	type=checkbox ' +
													'	id=option['+type+']['+row+'] ' +
													'	name=option['+type+']['+row+'] ' +
													'	value='+row+' ' +
														t+
													'	style="display:none"><div class=b alt="'+data.data[row]['desc']+'" trg='+data.data[row]['name']+'></div>' +
													'</div>');
									
								}
								
								
								$('.picto').unbind('click').click(function(){
									if($(this).hasClass('true'))
									{
										$(this)
										.removeClass('true')
										.find('input')
										.attr('checked',false);
									}else
									{
										$(this)
										.addClass('true')
										.find('input')
										.attr('checked',true);
									}
								});
								$('.b').each(function()
									{
										$(this).bubble({
											left:$(this).position().left,
											top:$(this).position().top,
											messege:$(this).attr('alt'),
											trigger:$(this).attr('trg')
											
									})
									
									});
							break;
						}
						
					}
				else
					{
						$(where).append( "Нет ни одного типа услуг.");
					}
			}
		});
}

function showOptions(type,id)
		{
			//////console.log(type+" "+id);
			switch(type)
			{
				case '1':
					//////console.log('Hotel');
					$('#options')
						.find('#option_context')
						.empty()
						.append('<input type="hidden" name="type" value="'+type+'">' +
								'<input type="hidden" name="id" value="'+id+'">' +
								'<input type="hidden" name="opttype" value="1">' +
								'<table border=1>' +
								'	<tr>' +
								'		<td valign=top>' +
								'			City'+
								'			<div id="sss"></div>' +
								'		</td>' +
								'		<td valign=top>' +
								'			stars' +
								'			<div id=stars></div>' +
								'		</td>' +
								'	</tr>' +
								'	<tr>' +
								'		<td  valign=top>' +
								'			Place' +
								'		</td>' +
								'		<td  valign=top>' +
								'			<div id=place></div>' +
								'		</td>' +
								'	</tr>' +
								'	<tr>' +
								'		<td  valign=top>' +
								'			in hotel' +
								'		</td>' +
								'		<td  valign=top>' +
								'			<div id=inhotel></div>' +
								'		</td>' +
								'	</tr>' +
								'	<tr>' +
								'		<td colspan=2>' +
								'			<div id=inhoteldesc></div>' +
								'		</td>' +
								'	</tr>' +
								'	<tr>' +
								'		<td  valign=top>' +
								'			in hotel room' +
								'		</td>' +
								'		<td  valign=top>' +
								'			<div id=inhotelroom></div>' +
								'		</td>' +
								'	</tr>' +
								'	<tr>' +
								'		<td colspan=2>' +
								'			<div id=inhotelroomdesc></div>' +
								'		</td>' +
								'	</tr>' +
								'</table>')
							.end()
						.toggle();
					loadList(13,id,'div#sss','radio');
					loadList(14,id,'div#stars','select');
					loadList(15,id,'div#place','text');
					loadList(16,id,'div#inhotel','picto');
					loadList(17,id,'div#inhoteldesc','text');
					loadList(18,id,'div#inhotelroom','picto');
					loadList(19,id,'div#inhotelroomdesc','text');
					//loadList(5,id,'div#sss','radio');
				break;
				case '2':
					//////console.log('Excursion');
					$('#options')
						.find('#option_context')
						.empty()
						.append('<input type="hidden" name="type" value="'+type+'">' +
								'<input type="hidden" name="id" value="'+id+'">' +
								'<input type="hidden" name="opttype" value="2">' +
								'<table border=1>' +
								'	<tr>' +
								'		<td valign=top>' +
								'			City'+
								'			<div id="sss"></div>' +
								'		</td>' +
								'		<td valign=top>' +
								'			Hotels' +
								'			<div id=hotel></div>' +
								'		</td>' +
								'		<td valign=top>' +
								'			Dostop' +
								'			<div id=dostop></div>' +
								'		</td>' +
								'	</tr>' +
								'	<tr>' +
								'		<td  valign=top align=center >' +
								'			Prodolj <div id=prod></div>' +
								'		</td>' +
								'		<td  valign=top align=center colspan=2>' +
								'			MaxPax <div id=maxp></div>' +
								'		</td>' +
								
								'	</tr>' +
								
								'</table>')
							.end()
						.toggle();
					loadList(29,id,'div#sss','checkbox');
					loadList(3,id,'div#hotel','checkbox');
					loadList(30,id,'div#dostop','checkbox');
					loadList(4,id,'div#prod','line');
					loadList(7,id,'div#maxp','line');
				break;
				case '3':
					//////console.log('Tour');
					$('#options')
						.find('#option_context')
						.empty()
						.append('<input type="hidden" name="type" value="'+type+'">' +
								'<input type="hidden" name="id" value="'+id+'">' +
								'<input type="hidden" name="opttype" value="3">' +
								'<table border=1>' +
								'	<tr>' +
								'		<td  valign=top>' +
								'			City'+
								'			<div id="city"></div>' +
								'		</td>' +
								'		<td valign=top>' +
								'			ekskurs' +
								'			<div id=ekskurs></div>' +
								'		</td>' +
								'		<td valign=top>' +
								'			Hotel' +
								'			<div id=hotel></div>' +
								'		</td>' +
								'	</tr>' +
								'	<tr>' +
								'		<td  valign=top colspan=3>' +
								'			Proj <div id=proj></div>' +
								'		</td>' +
								
								'	</tr>' +
									'	<tr>' +
								'		<td  valign=top colspan=3>' +
								'			Dni <div id=dni></div>' +
								'		</td>' +
								
								'	</tr>' +
									'	<tr>' +
								'		<td  valign=top colspan=3>' +
								'			Nochi <div id=nochi></div>' +
								'		</td>' +
								
								'	</tr>' +
									'	<tr>' +
								'		<td  valign=top colspan=3>' +
								'			Podarok <div id=podarok></div>' +
								'		</td>' +
								
								'	</tr>' +
								
								'</table>')
							.end()
						.toggle();
					loadList(31,id,'div#city','checkbox');
					loadList(2,id,'div#ekskurs','checkbox');
					loadList(8,id,'div#hotel','checkbox');
					loadList(1,id,'div#proj','text');
					loadList(5,id,'div#dni','line');
					loadList(6,id,'div#nochi','line');
					loadList(9,id,'div#podarok','text');
				break;
				case '4':
					//////console.log('showplace');
				break;
				case '5':
					//////console.log('City');
					//var context="";
					$('#options')
						.find('#option_context')
						.empty()
						.append('<input type="hidden" name="type" value="'+type+'">' +
								'<input type="hidden" name="id" value="'+id+'">' +
								'<input type="hidden" name="opttype" value="4">' +
								'<div id="showplaces">' +
								'ShowPlaces:' +
								'<div id="sss"></div>' +
								'</div>')
							.end()
						.toggle();
					loadList(10,id,'div#sss','checkbox');
				break;
			}
		}
$(function(){
		
		$('#clone').mwindow({
			trigger:'addTypeService',
			context:'<div id=tableid></div><input type="hidden" id="cloneId" name="cloneId" value="">Клонировать выделенную запись?',
			url:'/admin/cloneservice/',
			submit:true,
			width:'240',
			height:'100',
			name:'Клонировать запись',
			success:function(e){
				$('#clone').css('display','none');
				$("#table"+$('#tableid','#clone').attr('tableid')).trigger('reloadGrid');
			}
		});
		$('.button').each(function(){
			if($(this).attr('link')==window.location.pathname.substr(7))
			{
				$(this).removeClass('button').addClass('buttona');
			}
		}).click(function(){
			window.location ='/admin/'+$(this).attr('link');
		});
		/*$('.mError').mwindow({
			context:"<div id='errorMessage'></div>",
			width:'500',
			name:'<div id="nameError"></div>'
		});*/
		$('#masterError').mwindow({
			context:"<div id='errorMessage'></div>",
			trigger:'error',
			name:'<div id="nameError"></div>'
		});
		$('.modalW').mwindow({
			trigger:'addTypeService',
			context:'Пожалуйста введите название услуги:<br/><input type=text name="name" size=35 >',
			url:'/admin/addtype/',
			submit:true,
			width:'240',
			height:'100',
			name:'Добавить новый тип услуги',
			success:function(e){
				/*e=eval('('+e+')');
				if(e.state==true)
					{
						//////console.log('ok');
					}
				else
					{
						//////console.log('not ok');
					}*/
			}
		});
	
		$('.modalW2').mwindow({
			trigger:'addService',
			context:'Выберите тип услуги: <br/>',
			url:'/admin/addService/',
			submit:true,
			width:'240',
			height:'100',
			name:'Добавить новую услугу',
			success:function(e){
				$('#listType').append(e);
				
			}
		});
		//$('.modalW2').show();
		$.ajax({
			url: '/admin/getTypeList',
			type:'post',
			success:function(data)
			{
			
				data=eval('('+data+')');
				if(data.state==true)
					{
					////////console.log(obj);
						$('#mWFormId','.modalW2').append(data.context);
					}
				else
					{
						return "Нет ни одного типа услуг.";
					}
			}
		});
		$('#options').mwindow(
			{
				trigger:'addTypeService',
				context:'<div id="option_context" type=""></div>',
				url:'/admin/saveoptions/',
				submit:true,
				width:'500',
				height:'100',
				name:'Options',
				success:function(e){
					}
				
			});
		

});